$(document).ready(function () {
    courseType();
    findCourseTerm()
    $("#courseType").change(function () {
        var opt = $("#courseType").val();
        couresName(opt)
    });
});

//下拉列表获取课程类别
function courseType() {
    var cType = $("#courseType");

    $.ajax({
        url: "http://localhost:8080/mark/selectCourseType",
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            var str = "";
            d = data.data;
            ctype = d[1]
            for (x in ctype) {
                str += "<option value='" + ctype[x].courseType + "'>" + ctype[x].courseType + "</option>"
            }
            cType.append(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
}

//通过课程类型查看课程名称
function couresName(courseType) {
    var courseType = $("#courseType option:selected").val();
    /*if (courseType==null||courseType=="") {
        $("#courseName").empty()
        var aa=$("#courseName").val()
        alert("--"+aa)
        return false;
    }*/
    var courseName = $("#courseName")
    $.ajax({
        url: "http://localhost:8080/mark/selectCourseNameByCoursetype?courseType=" + courseType,
        type: "GET",
        async: false,
        dataType: "json",
        success: function (data) {
            // alert("aa")
            var str = "";
            d = data.data.courseList;
            for (x in d) {
                str += "<option value='" + d[x].courseName + "'>" + d[x].courseName + "</option>"
            }
            courseName.html(str);
        },
        error: function () {
            layer.alert("暂无记录")
        }
    })
};
//无条件查询所有课程
var findCourseTerm = function () {
    var url = "http://localhost:8080/mark/findCourseTerm";
    var table = $('#dataTable').dataTable()
    if (table) {
        table.fnDestroy();
    }
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.markList",
        "sAjaxSource": url,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                //"sDefaultContent": "<a href='AddMark.html?termYear=" + termYearAdd + "&termNum=" + termNumAdd + "&courseType=" + courseTypeAdd + "&courseName=" + courseNameAdd + "'><span  class='label label-danger mr10 mb10' id='write'>录入成绩</span></a>" +
                "sDefaultContent": "<a href='#' onclick='jumpToAddMark()'><span  class='label label-danger mr10 mb10' id='write'>录入成绩</span></a>" +
                "<a href='#'><span  class='label label-danger mr10 mb10' id='write'>更正</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "courseName"},
            {"mDataProp": "courseId"},
            {"mDataProp": "courseType"},
            {"mDataProp": "termYear"},
            {"mDataProp": "termNum"},
            {"mDataProp": "state",
                "mRender":function (data) {
                    //data=1录完，data=0没录
                    if (data == 0) {
                        return "<a href='#'><span  class='badge badge-danger mr10 mb10'  onclick='jumpToAddMark()' id='write'>成绩录入</span></a>";
                    }
                    if (data == 1) {
                        return "<span  class='badge badge-info mr10 mb10'>成绩已录完</span>"
                            +"<a href='#'><span  class='label label-danger mr10 mb10' id='update' onclick='jumpToUpdateMark()'>修改成绩</span></a>";
                    }

                }
            }
        ],
    })
}
//条件查询课程
//var findCourseTerm = function () {
$(document).on('click', '#search', function () {
    var termYear = $("#termYear option:selected").val();
    var termNum = $("#termNum option:selected").val();
    var courseType = $("#courseType option:selected").val();
    var courseName = $("#courseName").val();
    var url = "http://localhost:8080/mark/findCourseTerm?termYear=" + termYear + "&termNum=" + termNum + "&courseType=" + courseType + "&courseName=" + courseName;
    var table = $('#dataTable').dataTable()
    if (table) {
        table.fnDestroy();
    }
    $('#dataTable').dataTable({
        "bDeferRender": true,
        "sAjaxDataProp": "data.markList",
        "sAjaxSource": url,
        "sPaginationType": "bs_full", //"bs_normal", "bs_two_button", "bs_four_button", "bs_full"
        "fnPreDrawCallback": function (oSettings) {
            $('.dataTables_filter input').addClass('form-control input-large').attr('placeholder', 'Search');
            $('.dataTables_length select').addClass('form-control input-small');
        },
        "oLanguage": {
            "sSearch": "",
            "sLengthMenu": "<span>_MENU_ entries</span>",
            "sZeroRecords": "暂无相关信息",
        },
        "bJQueryUI": false,
        "bAutoWidth": false,
        "sDom": "<'row'<'col-lg-6 col-md-6 col-sm-12 text-center'l><'col-lg-6 col-md-6 col-sm-12 text-center'f>r>t<'row-'<'col-lg-6 col-md-6 col-sm-12'i><'col-lg-6 col-md-6 col-sm-12'p>>",
        "aoColumnDefs": [
            {
                "mDataProp": null,
                //"sDefaultContent": "<a href='AddMark.html?termYear=" + termYearAdd + "&termNum=" + termNumAdd + "&courseType=" + courseTypeAdd + "&courseName=" + courseNameAdd + "'><span  class='label label-danger mr10 mb10' id='write'>录入成绩</span></a>" +
                "sDefaultContent": /*"<a href='#' onclick='jumpToAddMark()'><span  class='label label-danger mr10 mb10' id='write'>录入成绩</span></a>" +*/
                "<a href='#'><span  class='label label-danger mr10 mb10' id='write'>更正</span></a>",
                "aTargets": [-1]
            }
        ],
        "aoColumns": [
            {"mDataProp": "courseName"},
            {"mDataProp": "courseId"},
            {"mDataProp": "courseType"},
            {"mDataProp": "termYear"},
            {"mDataProp": "termNum"},
            {"mDataProp": "state",
                "mRender":function (data) {
                //data=1录完，data=0没录
                    if (data == 0) {
                        return "<a href='#'><span  class='badge badge-danger mr10 mb10'  onclick='jumpToAddMark()' id='write'>成绩录入</span></a>";
                    }
                    if (data == 1) {
                        return "<span  class='badge badge-info mr10 mb10'>成绩已录完</span>"
                            +"<a href='#'><span  class='label label-danger mr10 mb10' id='update' onclick='jumpToUpdateMark()'>修改成绩</span></a>";
                    }

                }
            }
        ],
    })
})
//};
//点击“录入”调用该函数
function jumpToAddMark() {
  //  alert("aaa")
    $(document).on('click', '#write', function () {
        //alert("0000")
        var courseNameAdd = $(this).parents("tr").children(0).eq(0).text();
        var courseNumAdd = $(this).parents("tr").children(0).eq(1).text();
        var courseTypeAdd = $(this).parents("tr").children(0).eq(2).text();
        var termYearAdd = $(this).parents("tr").children(0).eq(3).text();
        var termNumAdd = $(this).parents("tr").children(0).eq(4).text();
       // alert("courseNameAdd:" + courseNameAdd)
        /*alert("courseNumAdd:" + courseNumAdd)
        alert("courseTypeAdd:" + courseTypeAdd)
        alert("termYearAdd:" + termYearAdd)
        alert("termNumAdd:" + termNumAdd)*/
        location.href='AddMark.html?termYear='+termYearAdd+
                                        '&termNum='+encodeURI(encodeURI(termNumAdd))+
                                        '&courseType='+encodeURI(encodeURI(courseTypeAdd)) +
                                        '&courseName=' + encodeURI(encodeURI(courseNameAdd))+
                                        '&courseNum='+courseNumAdd+'';
    })

}
//点击“更新”调用此函数
function jumpToUpdateMark(){
    $(document).on('click', '#update', function () {
        //alert("0000")
        var courseNameUpdate = $(this).parents("tr").children(0).eq(0).text();
        var courseNumUpdate = $(this).parents("tr").children(0).eq(1).text();
        var courseTypeUpdate = $(this).parents("tr").children(0).eq(2).text();
        var termYearUpdate = $(this).parents("tr").children(0).eq(3).text();
        var termNumUpdate = $(this).parents("tr").children(0).eq(4).text();
       /* alert("courseNameUpdate:" + courseNameUpdate)
        alert("courseNumUpdate:" + courseNumUpdate)
        alert("courseTypeUpdate:" + courseTypeUpdate)
        alert("termYearUpdate:" + termYearUpdate)
        alert("termNumUpdate:" + termNumUpdate)*/
        location.href='UpdateMark.html?termYear='+termYearUpdate+
            '&termNum='+encodeURI(encodeURI(termNumUpdate))+
            '&courseType='+encodeURI(encodeURI(courseTypeUpdate)) +
            '&courseName=' + encodeURI(encodeURI(courseNameUpdate))+
            '&courseNum='+courseNumUpdate+'';
    })
}
